<template>
  <view class="content">
    <!-- 搜索组件 -->
    <view class="content-search">
      <search @tap="toSearch"></search>
    </view>
    <!-- 轮播图区域 -->
    <swiper 
    class="content-swiper" 
    indicator-dots
    autoplay
    circular
    :interval="3000"
    >
      <swiper-item v-for="item in swiperList" :key="item.goods_id">
        <image :src="item.image_src" @tap="toGoodsDetail(item.goods_id)"></image>
      </swiper-item>
      </swiper>
      <!-- 分类导航区域 -->
      <view class="content-nav">
        <view class="content-nav-item" v-for="(item,index) in navList" :key="index" @tap="toCate(index)">
          <image :src="item.image_src" ></image>
        </view>
      </view>
      <!-- 楼层区域 -->
      <view class="content-floor">
        <view class="content-floor-item" v-for="(item,index) in floorList" :key="index">
          <image :src="item.floor_title.image_src" ></image>
          <!-- 楼层图片的区域 -->
          <view class="imgBox">
            <!-- 左侧大图片区域 -->
            <view class="leftImg" @tap="toGoodsList(item.product_list[0].url)">
              <image :src="item.product_list[0].image_src" :style="{width:item.product_list[0].image_width + 'rpx' }" mode="widthFix"></image>
            </view>
            <!-- 右侧4个小图片的区域 -->
            <view class="rightImg">
              <view  v-for="(item2, index2) in item.product_list" :key="index2" v-if="index2 !== 0" @tap="toGoodsList(item2.url)">
                <image :src="item2.image_src" :style="{width:item2.image_width + 'rpx'}" mode="widthFix"></image>
              </view>
            </view>
          </view>
        </view>
      </view>
  </view>
</template>

<script>
  import cartBadge from '@/mixins/cartTabbar-badge.js'
  import {
    NAV_TO,
    NAV_TAB,
    TOAST_SHOW_SUCCESS,
    TOAST_SHOW_FAIL
  } from '@/utils/view_utils.js'
  import {
    API_SwiperData,
    API_Catitems,
    API_Floordata
  } from '@/api/API.js'
  export default {
    mixins: [cartBadge],
    data() {
      return {
        swiperList: [],
        navList: [],
        floorList: []
      }
    },
    onLoad() {
      this.getSwiperList();
      this.getNavList();
      this.getFloorList();
    },
    methods: {
      // 获取轮播图数据
      async getSwiperList() {
        const res = await API_SwiperData();
        if(res.data.meta.status !== 200){
          return TOAST_SHOW_FAIL("轮播图数据请求失败")
        }
        this.swiperList = res.data.message;
      },
      // 获取分类导航栏数据
      async getNavList() {
        const res = await API_Catitems();
        
        if(res.data.meta.status !== 200){
          return TOAST_SHOW_FAIL("导航栏数据请求失败")
        }
        this.navList = res.data.message;
      },     
      // 获取楼层数据
      async getFloorList() {
        const res = await API_Floordata();
        
        if(res.data.meta.status !== 200){
          return TOAST_SHOW_FAIL("楼层数据请求失败")
        }
        res.data.message.forEach(floor => {
          floor.product_list.forEach(item => {
            item.url = '/subpkg/goods_list?' + item.navigator_url.split('?')[1];
          })
        })
        this.floorList = res.data.message;
      },
      // 去商品详情页
      toGoodsDetail(id) {
        NAV_TO("/subpkg/goods_detail/goods_detail",{
          goods_id: id,
        })
      },
      // 点击分类导航去分类页面
      toCate(index) {
        if(index == '0') {
          NAV_TAB("/pages/cate/cate")
        }
      },
      // 点击楼层图片跳转至商品列表
      toGoodsList(url) {
        NAV_TO(url)
      },
      // 点击搜索组件时间
      toSearch(){
        NAV_TO('/subpkg/search/search')
      }
    }
  }
</script>

<style lang="scss" scoped>
.content {
  &-search {
    position: sticky;
    top: 0;
    z-index: 999;
  }
  &-swiper {
    image {
      width: 100%;
    }
  }
  &-nav {
    display: flex;
    justify-content: space-around;
    margin: 30rpx 0;
    image {
      width: 128rpx;
      height: 140rpx;
    }
  }
  &-floor {
    image {
      height: 60rpx;
      width: 100%;
      display: flex;
    }
    .imgBox {
      display: flex;
      padding-left: 10rpx;
      .rightImg {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-around;
      }
    }
  }
}
</style>
